//
// Created by mio on 25-5-12.
//

#ifndef GDICAPTURE_H
#define GDICAPTURE_H

#include <QThread>
#include <memory>
#include <mutex>
#include <atomic>

#include "VideoCapture.h"

struct AVFrame;
struct AVPacket;
struct AVInputFormat;
struct AVCodecContext;
struct AVFormatContext;
using FrameContainer = std::vector<quint8>;


/// Init初始化之后开始捕获, 在run中每25ms开始捕获一帧, GetOnewFrame中如果得到的视频帧就进行解码, 解码
/// 得到的数据会放在 rgba_frame_ 中, 外界通过CaptureFrame接口将数据取走
class GDICapture : public VideoCapture
{
public:
    GDICapture();
    GDICapture(const GDICapture&) = delete;
    GDICapture& operator=(const GDICapture&) = delete;
    virtual ~GDICapture();
public:
    bool Init(qint32 display_index = 0, const QRect& capture_region = QRect()) override;
    bool Close() override;
    bool CaptureFrame(AVFramePtr& bgra_frame) override;
protected:
    void run() override;
private:
    void StopCapture();
    bool GetOneFrame();
    bool Decode(AVFrame* av_frame,AVPacket* av_packet);
private:
    using framPtr = std::shared_ptr<quint8>;
    quint32 frame_size_;
    framPtr rgba_frame_;
    qint64  video_index_;
    std::mutex mutex_;
    AVInputFormat* input_format_;
    AVCodecContext* codec_context_;
    AVFormatContext* format_context_;
    std::shared_ptr<AVFrame> av_frame_;
    std::shared_ptr<AVPacket> av_packet_;


};

#endif //GDICAPTURE_H